package com.zc.angelica.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zc.angelica.entity.Account;
import com.zc.angelica.entity.PlatformInfo;
import com.zc.angelica.entity.ResponseData;
import com.zc.angelica.mapper.AccountPlatformInfoMapper;
import com.zc.angelica.entity.AccountPlatformInfo;
import com.zc.angelica.service.AccountPlatformInfoService;
import com.zc.angelica.service.PlatformInfoService;
import com.zc.angelica.utils.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 账户平台关系表(AccountPlatformInfo)表服务实现类
 *
 * @author makejava
 * @since 2022-04-10 18:33:08
 */
@Service
public class AccountPlatformInfoServiceImpl extends ServiceImpl<AccountPlatformInfoMapper, AccountPlatformInfo> implements AccountPlatformInfoService {

    @Autowired
    private PlatformInfoService platformInfoService;

    @Override
    public ResponseData bindingPlatform(String platformCode) {
        Account accountInfo = SecurityUtil.getAccountInfo();
        if (accountInfo == null) {
            return ResponseData.error("未获取到登录信息");
        }
        QueryWrapper<AccountPlatformInfo> accountPlatformInfoQueryWrapper = new QueryWrapper<>();
        accountPlatformInfoQueryWrapper.eq("account_id",accountInfo.getId()).eq("platform_code",platformCode);
        Long platformCount = this.baseMapper.selectCount(accountPlatformInfoQueryWrapper);
        if (platformCount>0){
            return ResponseData.error("平台已经绑定请勿重复绑定");
        }
        QueryWrapper<PlatformInfo> platformInfoQueryWrapper = new QueryWrapper<>();
        platformInfoQueryWrapper.eq("platform_code",platformCode);
        PlatformInfo platformInfo = platformInfoService.getBaseMapper().selectOne(platformInfoQueryWrapper);
        if (platformInfo==null){
            return ResponseData.error("平台不存在");
        }
        AccountPlatformInfo accountPlatformInfo = new AccountPlatformInfo();
        accountPlatformInfo.setAccountId(accountInfo.getId());
        accountPlatformInfo.setPlatformCode(platformCode);
        accountPlatformInfo.setPlatformId(platformInfo.getId());
        accountPlatformInfo.setUsername(accountInfo.getUsername());
        boolean save = this.save(accountPlatformInfo);
        if (save) {
            return ResponseData.success("绑定成功");
        }
        return ResponseData.error("绑定失败");
    }

    @Override
    public ResponseData unbindingPlatform(Long id) {
        boolean remove = this.removeById(id);
        if (remove) {
            return ResponseData.success("解绑成功");
        }
        return ResponseData.error("解绑失败");
    }

    @Override
    public List<PlatformInfo> selectAllPlatform() {
        Account accountInfo = SecurityUtil.getAccountInfo();
        if (accountInfo == null) {
            return null;
        }
        List<PlatformInfo> platforms = platformInfoService.list();
        QueryWrapper<AccountPlatformInfo> accountPlatformInfoQueryWrapper = new QueryWrapper<>();
        accountPlatformInfoQueryWrapper.eq("account_id", accountInfo.getId());
        List<AccountPlatformInfo> myPlatforms = this.list(accountPlatformInfoQueryWrapper);
        if (myPlatforms.size()>0){
            List<String> myPlatformCodes = myPlatforms.stream().map(AccountPlatformInfo::getPlatformCode).collect(Collectors.toList());
            platforms.forEach(itme -> {
               if (myPlatformCodes.contains(itme.getPlatformCode())){
                   itme.setBinding(2);
               };
            });
        }
        return platforms;
    }
}

